﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ProductList : System.Web.UI.Page
{
    int CurrentPage;//当前页数
    int PageSize;   //每页条数
    int PageCount;  //总页数
    int RecordCount;//总条数
    int type = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        PageSize = 12;
        if (!Page.IsPostBack)
        {
            
           object param = Request.Params["type"];
           if (param != null)
           {
               type = int.Parse(param.ToString());
           }
           else
               type = 1;
           switch (type)
           {
               case 1:
                   Master.Page.Title = "热卖商品";
                   break;
               case 2:
                   Master.Page.Title = "精品推荐";
                   break;
               case 3:
                   Master.Page.Title = "特价商品";
                   break;
           
           }
            CurrentPage = 0;//当前页习惯设为0
            ViewState["PageIndex"] = 0;//页索引也设为0


            //计算总共有多少记录
            RecordCount = getCount(type);


            //计算总共有多少页
            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }
           if (PageCount == 0)
                PageCount = 1;
            this.TotalLbl.Text = PageCount.ToString();//显示总页数
            
            ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ，而视图状态 viewstate相当于某个页面的 session

            this.DataListBind();//不可以放在初始化条件之前就绑定，那样的话，如果仅有一页的数据，“下一页”页仍然显示


        }
    }
    /// <summary>
    /// 根据类型计算总记录数
    /// </summary>
    public int getCount(int type)
    {
        DbAccess db = new DbAccess();
        string sql = "";
        switch (type)
        {
            case 1:
                sql = "select count(1) from Product where IsHot =1 ";
                break;
            case 2:
                sql = "select count(1) from product where IsFaience=1";
                break;
            default:
                sql = "select count(1) from product where IsSale=1";
                break;
        
        }
        DataSet dsProCnt = db.GetDataSet(sql);
        int count = 0; 
        if(dsProCnt.Tables[0].Rows.Count>0)
            count=int.Parse(dsProCnt.Tables[0].Rows[0][0].ToString());

        return count;
        //2*(this.AspNetPager1.CurrentPageIndex-1) 一下页从那条记录开始，2提取的记录最大数也就是分页的条数
    }
    public void DataListBind()
    {
        try
        {
            string sql = "";
            switch (type)
            {
                case 1:
                    sql = "select * from Product where IsHot =1 ";
                    break;
                case 2:
                    sql = "select * from product where IsFaience=1";
                    break;
                default:
                    sql = "select * from product where IsSale=1";
                    break;

            }
            DbAccess db=new DbAccess();
            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
            DataSet ds = db.getPage(sql, StartIndex, 12);
            dlLst.DataSource = ds.Tables[0].DefaultView;
            dlLst.DataBind();
            this.PreviousLB.Enabled = true;
            this.NextLB.Enabled = true;
            if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时，下一页链接按钮不可用
            if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时，上一页按钮不可用
            this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数

        }


        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }


    public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
    {
        CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
        PageCount = (int)ViewState["PageCount"];//获得总页数


        string cmd = e.CommandName;

        //判断cmd，以判定翻页方向


        switch (cmd)
        {
            case "prev"://上一页
                if (CurrentPage > 0) CurrentPage--;
                break;

            case "next":
                if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                break;

            case "first"://第一页
                CurrentPage = 0;
                break;

            case "end"://最后一页
                CurrentPage = PageCount - 1;
                break;

            case "jump"://跳转到第几页
                if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                {
                    return;
                }
                else
                {
                    CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                    break;
                }
        }
        ViewState["PageIndex"] = CurrentPage;//获得当前页

        DataListBind();//重新将DataList绑定到数据库


    }


    protected void dlLst_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "add")
        {
            Session["Id"] = ((Label)e.Item.FindControl("Id")).Text;//商品编号
            Session["Proname"] = ((Label)e.Item.FindControl("Proname")).Text;//商品名称
            Session["MemberPrice"] = ((Label)e.Item.FindControl("MemberPrice")).Text;//商品单价
            Response.Redirect("Buy.aspx");
        }
    }
}
